﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Services;
using Module;
using Module.QuanLyDuoc.HeThongNhapXuat;

namespace HospitalManager.Webservice.QuanLyDuoc.HeThongNhapXuat
{
    /// <summary>
    ///     Summary description for wsPhieuNhapVatTu
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [ScriptService]
    public class wsPhieuNhapVatTu : WebService
    {
        private static readonly PhieuNhapVatTu _phieuNhapVatTu = new PhieuNhapVatTu();

        [WebMethod(EnableSession = true)]
        public string PhieuNhapVatTu_Insert(string sochungtu, string ngaychungtu, string makho, int manhacungcap, string ngaygiao,
            string nguoigiao, string nguoinhan, int thue, string diengiai, string nguoitao)
        {
            var obj = new PhieuNhapVatTu
            {
                SoChungTu = sochungtu,
                NgayChungTu =
                    (string.IsNullOrEmpty(ngaychungtu)
                        ? (DateTime?)null
                        : Convert.ToDateTime(DateTime.ParseExact(ngaychungtu, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"))),
                MaKho = makho,
                MaNhaCungCap = manhacungcap,
                NgayGiao = Convert.ToDateTime(DateTime.ParseExact(ngaygiao, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiGiao = nguoigiao,
                NguoiNhan = nguoinhan,
                Thue = thue,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuNhapVatTu.PhieuNhapVatTu_Insert(obj) ? obj.MaPhieuNhap : string.Empty;
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuNhapVatTu_Update(string maphieunhap, string sochungtu, string ngaychungtu, string makho, int manhacungcap,
            string ngaygiao, string nguoigiao, string nguoinhan, int thue, string diengiai, string nguoitao)
        {
            var obj = new PhieuNhapVatTu
            {
                MaPhieuNhap = maphieunhap,
                SoChungTu = sochungtu,
                NgayChungTu =
                    (string.IsNullOrEmpty(ngaychungtu)
                        ? (DateTime?)null
                        : Convert.ToDateTime(DateTime.ParseExact(ngaychungtu, "dd-MM-yyyy", null).ToString("yyyy-MM-dd"))),
                MaKho = makho,
                MaNhaCungCap = manhacungcap,
                NgayGiao = Convert.ToDateTime(DateTime.ParseExact(ngaygiao, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiGiao = nguoigiao,
                NguoiNhan = nguoinhan,
                Thue = thue,
                DienGiai = diengiai,
                NguoiTao = nguoitao
            };
            return _phieuNhapVatTu.PhieuNhapVatTu_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuNhapVatTu_SelectById(string maphieunhap)
        {
            return JsonMethods.ToJsonString(_phieuNhapVatTu.PhieuNhapVatTu_SelectById(maphieunhap));
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuNhapVatTu_ChiTiet_Insert(string maphieunhap, string mavattu, string madonvi, int soluong, double dongia,
            int nguongocxuatxu, string losanxuat, string ngaysanxuat, string hansudung, string nguoitao)
        {
            var obj = new PhieuNhapVatTu
            {
                MaPhieuNhap = maphieunhap,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                NguonGocXuatXu = nguongocxuatxu,
                LoSanXuat = losanxuat,
                NgaySanXuat = Convert.ToDateTime(DateTime.ParseExact(ngaysanxuat, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiTao = nguoitao
            };
            return _phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_Insert(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuNhapVatTu_ChiTiet_Update(string id, string maphieunhap, string mavattu, string madonvi, int soluong, double dongia,
            int nguongocxuatxu, string losanxuat, string ngaysanxuat, string hansudung, string nguoitao)
        {
            var obj = new PhieuNhapVatTu
            {
                ID = id,
                MaPhieuNhap = maphieunhap,
                MaVatTu = mavattu,
                MaDonVi = madonvi,
                SoLuong = soluong,
                DonGia = dongia,
                NguonGocXuatXu = nguongocxuatxu,
                LoSanXuat = losanxuat,
                NgaySanXuat = Convert.ToDateTime(DateTime.ParseExact(ngaysanxuat, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                HanSuDung = Convert.ToDateTime(DateTime.ParseExact(hansudung, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                NguoiTao = nguoitao
            };
            return _phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_Update(obj);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuNhapVatTu_ChiTiet_Delete(string id)
        {
            return _phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_Delete(id);
        }

        [WebMethod(EnableSession = true)]
        public string PhieuNhapVatTu_ChiTiet_SelectById(string id)
        {
            return JsonMethods.ToJsonString(_phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_SelectById(id));
        }
        [WebMethod(EnableSession = true)]
        public string PhieuNhapVatTu_ChiTiet_SelectByMaPhieuNhap(string maphieunhap, string textsearch, int offset, int limit)
        {
            int total;
            var ds = new DataSet();
            var dtRow = _phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_SelectByMaPhieuNhap(maphieunhap, textsearch, offset, limit, out total).Copy();
            ds.Tables.Add(dtRow);
            var dtTotal = new DataTable();
            dtTotal.Columns.Add("ToTalRow", typeof(string));
            dtTotal.Rows.Add(total.ToString(CultureInfo.InvariantCulture).Trim());
            ds.Tables.Add(dtTotal);
            return JsonMethods.ToJsonString(ds);
        }

        [WebMethod(EnableSession = true)]
        public bool PhieuNhapVatTu_ChiTiet_UpdateDonGia_TinhTrang(string id, double dongiaxuatdv, double dongiaxuatbh,
            double phantrambhthanhtoan, string doituongbh, int issudung)
        {
            var obj = new PhieuNhapVatTu
            {
                ID = id,
                DonGiaXuatDV = dongiaxuatdv,
                DonGiaXuatBH = dongiaxuatbh,
                PhanTramBHThanhToan = phantrambhthanhtoan,
                DoiTuongBH = doituongbh,
                IsSuDung = issudung
            };
            return _phieuNhapVatTu.PhieuNhapVatTu_ChiTiet_UpdateDonGia_TinhTrang(obj);
        }
    }
}